#include "all_lib.h"
#pragma hdrstop

#if defined(__GNUC__)
  #include "Std/xfuncs.inc"
#endif

char str[ 10000 ];
BOOL Quite = FALSE;

int Print( const char *PrintfFormat,... )
  {  va_list  a;
     int      sz;

     if (Quite) return 0;

     va_start( a,PrintfFormat );
       sz = vfprintf( stderr, PrintfFormat, a );
     va_end( a );

 return sz;
}

void FClose( FILE *f, CONSTSTR fnm )
  {
     if ( !f ) return;

     if ( CTArgCheck( "GZ" ) )
       system( Message("gzip %s", fnm ) );
      else
       ;
}

void ParceFile( FILE *f, int ns )
  {  char *m;
     char  fnm[ MAX_PATH_SIZE ];
     int   cn = -1,
           pcn = 0;
     FILE *out = NULL;

     int nl = -1,
         nsz = -1;

     if ( (m=CTArgGet( "NLINES;NL" )) != NULL ) {
       nl = Str2DigitDetect( m, 10, -1 );
       nl = Max( 10, nl );
     } else
     if ( (m=CTArgGet( "NS;NSIZE" )) != NULL ) {
       nsz = Str2DigitDetect( m, 10, -1 );
       nsz = Max( 10, nsz );
     } else
       nl = 1000;

     if ( nl != -1 )
       Print( "Portion: %d lines\n", nl );
      else
     if ( nsz != -1 )
       Print( "Portion: %d KBytes\n", nsz );

     while( fgets( str, sizeof(str), f ) != NULL && !feof(f) ) {
       m = strchr( str, '\n' ); if ( m ) *m = 0;
       m = strchr( str, '\r' ); if ( m ) *m = 0;
       for( m = str; *m && isspace(*m); ) m++;
       if ( !m[0] ) continue;

       cn++;
       if ( cn < ns ) continue;

       if ( nl != -1 ) {
         if ( out && pcn >= nl ) {
           FClose( out, fnm );
           out = NULL;
           pcn = 0;
         }
       } else
       if ( nsz != -1 ) {
         if ( out && ftell(out) >= nsz*1000 ) {
           FClose( out, fnm );
           out = NULL;
           pcn = 0;
         }
       }

       if ( !out ) {
         m = strchr( str, '.' );
         if ( !m ) {
           Print( "Invalid line read: [%s]!\n",str );
           continue;
         }

         PRTime tm;
         *m = 0;
         tm.Set( (time_t)atoi(str) );
         *m = '.';

         if ( tm.isNull() ) {
           Print( "Invalid line read: [%s]!\n",str );
           continue;
         }

         sprintf( fnm, "From_%02d-%02d-%4d_%02d_%02d_%02d.log",
                  tm.mday, tm.mon, tm.year, tm.hour, tm.min, tm.sec );
         Print( "Create file part: %s\n", fnm );
         out = fopen( fnm,"w+" );
         if ( !out )
           HAbort( "!open dest file: %s!", fnm );
       }

       fprintf( out, "%s\n", str );
       pcn++;
     }
     if ( out )
       FClose( out, fnm );
}

int main(int argc,char *argv[])
  {
     CTArgInit(argc,argv,FALSE);

     if (argc < 2 || CTArgCheck("?") ) {
       Print( "USAGE: squ <command> [<options>]\n"
               " Commands:\n"
               "   -time=<stamp>\n"
               "     Decode squid time stamp\n"
               "\n"
               "   -split=<file> [<nlines>=<cn> | <nsize>=<Kb>] [start=<val>] [from=<tm>] [to=<tm>] [GZ]\n"
               "     Split text <file> by <nlines> portions and write them into another files\n"
               "     NLines=<val>  - number of lines in single portion (1000)\n"
               "     NSize=<val>   - limit portion by output file size\n"
               "     STart=<val>   - skip <val> first portions\n"
               "     From=<val>    - skip lines until specified time\n"
               "     To=<val>      - stop parce after first time greater then <val>\n"
               "     GZ            - GZIP output portion\n"
               "\n"
               " Options:\n"
               "   -Quite          - quite output\n"
               "\n"
               " Samples:\n"
               "   squ -split=access.0.log -nsize=100 -gz\n"
               "         Splits squid log by 100K portions and GZIP them\n"
               "   squ.exe -time=1193508017\n"
               "         Decode squid log timestamp\n"
               "" );
       return 0;
     }

     setbuf( stdout, 0 );
     setbuf( stderr, 0 );

     CONSTSTR m;

     Quite = CTArgCheck( "Q" ) || CTArgCheck( "QUITE" );

   //Time
     if ( (m=CTArgGet("TIME")) != NULL ) {

       time_t tm = (time_t)atoi( m );

       strftime( str, sizeof(str),"%d/%m/%Y %H:%M:%S",localtime(&tm) );
       printf( "Time for [%s] is [%s]\n",m, str );
       return 0;
     }

   //Split
     if ( (m=CTArgGet("SPLIT")) != NULL ) {
       FILE *f = fopen( m, "rt" );
       if ( !f ) HAbort( "!open source file: %s", m );

       int nStart;

       m = CTArgGet( "ST;START" );
       if ( m ) nStart = Str2DigitDetect( m, 10, -1 ); else nStart = 0;
       nStart = Max( 0, nStart );
       Print( "Start from: %d line\n", nStart );

       ParceFile( f, nStart );
       return 0;
     }

 return 0;
}
